Skip to content

cmd/goyacc: don't panic on grammar files ending without a newline - #671

Open
shaggyinsomniac wants to merge 1 commit into
golang:masterfrom
shaggyinsomniac:fix/goyacc-eof-unget-panic
Open

cmd/goyacc: don't panic on grammar files ending without a newline#671
shaggyinsomniac wants to merge 1 commit into
golang:masterfrom
shaggyinsomniac:fix/goyacc-eof-unget-panic

Conversation

@shaggyinsomniac

Copy link
Copy Markdown

Fixes golang/go#81182.

getrune keeps EOF sticky in the single-slot peekrune buffer (it returns EOF without clearing the slot). When gettok's IDENTIFIER/IDENTCOLON look-ahead reaches end-of-file and ungets the EOF rune, ungetrune hits its single-slot "2nd unget" panic — a 10-byte grammar with no trailing newline crashes:

$ printf '%%\nS: A; B' > v2.y && goyacc -o /dev/null v2.y
panic: ungetc - 2nd unget

goroutine 1 [running]:
main.ungetrune(...)
	cmd/goyacc/yacc.go:3160
main.gettok()
	cmd/goyacc/yacc.go:990

Make ungetting EOF a no-op: an exhausted reader returns EOF again on the next read regardless, so behavior for well-formed files is unchanged. With the fix the example reports the proper illegal rule: missing semicolon or | error and exits 1, and a valid grammar compiles to identical output.

getrune keeps EOF sticky in peekrune (it returns EOF without clearing
the slot), so when gettok's IDENTIFIER/IDENTCOLON look-ahead reaches
end-of-file and ungets the EOF rune, ungetrune hit its single-slot
"2nd unget" panic. A 10-byte grammar such as

    %%
    S: A; B

(with no trailing newline) crashed with

    panic: ungetc - 2nd unget

Make ungetting EOF a no-op: an exhausted reader returns EOF again on
the next read regardless, so behavior for well-formed files is
unchanged. The example above now reports the proper syntax error
("illegal rule: missing semicolon or |"), and a valid grammar
compiles to identical output.
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 642be10) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/823504.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/823504.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 1: Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/823504.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-09-01T17:06:23Z","revision":"ae26d2c4381592fcbc8cc1df7c89b1049ea1857e"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/823504.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 1: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_3771561>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/823504.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/823504.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/823504.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

x/tools/cmd/goyacc: panic ("ungetc - 2nd unget") on grammar file ending without a trailing newline

2 participants